<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="rules"
      label-width="100px"
    >
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门" prop="departmentName">
            <el-input
              v-model="userInfo.departmentName"
              @focus="showTree = true"
            />
            <Tree :visible.sync="showTree" @current-select="getDept" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 手机号 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机号" prop="timeOfEntry">
            <el-input v-model="userInfo.mobile" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <UploadImg :staff-photo.sync="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSaveUserDetailById">
            保存更新
          </el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { saveUserDetailById } from '@/api/employees'
export default {
  props: {
    userInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      showTree: false, // 部门树形结构
      rules: {
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    hSaveUserDetailById () {
      this.$refs.userForm.validate(async flag => {
        if (!flag) return
        // 使用最新修改数据覆盖源数据 作为接口提交数据
        await saveUserDetailById(this.userInfo)
        this.$message.success('更新成功')
      })
    },
    // 点击部门树形结构时 赋值部门数据
    getDept (val) {
      // 给部门赋值
      this.userInfo.departmentName = val.name
      // 关闭树形结构
      this.showTree = false
    }
  }
}
</script>
